package com.karrysong.blog_springboot.controller;

import com.karrysong.blog_springboot.service.UserService;
import com.karrysong.blog_springboot.vo.Result;
import com.karrysong.blog_springboot.vo.params.LoginParam;
import com.karrysong.blog_springboot.vo.params.UserPage;
import com.karrysong.blog_springboot.vo.params.UserParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @ClassName: UserController
 * @Author: KarrySong
 * @Date: 2022/8/4 9:35
 * @Description:
 */
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/currentUser")
    public Result currentUser(@RequestHeader("Authorization") String token){

        return userService.getUserInfoByToken(token);
    }

    @PostMapping("/login")
    public Result login(@RequestBody LoginParam loginParam){

        return userService.login(loginParam);
    }

    @GetMapping("/logout")
    public Result logout(@RequestHeader("Authorization") String token){
        return userService.logout(token);
    }

    /*@PostMapping("/register")
    public Result register(@RequestBody LoginParam loginParam){
        return userService.register(loginParam);
    }*/

    @PostMapping("/register")
    public Result register(@RequestBody UserParam userParam){
        return userService.register(userParam);
    }

    @PostMapping("/update")
    public Result update(@RequestBody UserParam userParam){
        return userService.update(userParam);
    }



    @PostMapping("/admin/all")
    public Result getUsers(@RequestBody UserPage userPage){
        if (userPage == null){
            userPage = new UserPage(1, 5);
        }
        return userService.getUserList(userPage);
    }

    @PostMapping("/admin/{id}/{newStatus}")
    public Result changeStatus(@PathVariable("id") Long id, @PathVariable("newStatus") Integer newStatus){
        return userService.changeUserStatus(id, newStatus);
    }

    @PostMapping("/admin/delete/{id}")
    public Result deleteArticle(@PathVariable("id") Long id){
        return userService.deleteUser(id);
    }

}
